@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">添加页面</div>

                <div class="panel-body">
                    <form id="upload" method="POST" action="{{ route('page.store') }}" enctype="multipart/form-data">
                        {{ csrf_field() }}
                        <div class="form-group">
                            <label for="inputName">名称</label>
                            <input type="text" class="form-control" id="inputName" placeholder="名称" name="name">
                        </div>
                        <div class="form-group">
                            <label for="inputEntry">入口文件</label>
                            <input type="text" class="form-control" id="inputEntry" placeholder="index.html"
                                   name="entry" value="index.html">
                        </div>
                        <div class="form-group">
                            <label for="inputTag">标签</label>
                            <input type="text" class="form-control" id="inputTag" placeholder="标签" name="tag">
                        </div>
                        <div id="tag" class="form-group">
                            <span class="label label-info" style="cursor: pointer">列表页</span>
                            <span class="label label-primary" style="cursor: pointer">图文详情</span>
                            <span class="label label-success" style="cursor: pointer">关于页</span>
                            <span class="label label-default" style="cursor: pointer">图文列表</span>
                            <span class="label label-warning" style="cursor: pointer">表单页</span>
                            <span class="label label-danger" style="cursor: pointer">视频页</span>
                            <span class="label label-success" style="cursor: pointer">视频列表</span>
                        </div>
                        <div class="form-group">
                            <label for="inputName">版本</label>
                            <input type="text" class="form-control" id="inputName" placeholder="1.0" value="1.0"
                                   name="version">
                        </div>
                        <div class="form-group">
                            <label for="inputFile">选择文件（*.zip）</label>
                            <input type="file" id="InputFile" name="file">
                            <p class="help-block">仅支持zip上传</p>
                        </div>
                        <button type="button" class="btn btn-default" onclick="upload()">提交</button>
                    </form>

                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('script')
<script>
    $('#tag span').click(function () {
        var tagValue = $('[name="tag"]').val();
        if(tagValue){
            tagValue = JSON.parse(tagValue);
        }else {
            tagValue=[]
        }
        if($.inArray($(this).text(),tagValue) !== -1){
            tagValue.splice($.inArray($(this).text(),tagValue),1);
        }else {
            tagValue.push($(this).text());
        }
        $.unique(tagValue);
        $('[name="tag"]').val(JSON.stringify(tagValue))
    })

    function upload() {
        var form=$( "#upload" )[0];
        var formData = new FormData(form);
        $.ajax({
            url: form.action,
            type: 'POST',
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (result) {
                layer.msg(result.message);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest, textStatus, errorThrown);
                ajaxError(XMLHttpRequest, textStatus, errorThrown)
            }
        });
    }
</script>
@endsection